package com.iotechn.unimall.admin.api.discuss;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.google.gson.Gson;
import com.iotechn.unimall.core.exception.AdminServiceException;
import com.iotechn.unimall.core.exception.ExceptionDefinition;
import com.iotechn.unimall.core.exception.ServiceException;
import com.iotechn.unimall.data.domain.ShopDiscussDO;
import com.iotechn.unimall.data.mapper.ShopDiscussMapper;
import com.iotechn.unimall.data.model.Page;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;


@Service
public class AdminShopDiscussServiceImpl implements AdminShopDiscussService {
    @Autowired
    private ShopDiscussMapper shopDiscussMapper;

 

    @Override
    public Page<ShopDiscussDO> list(Integer page, Integer limit,  Long adminId) throws ServiceException {
        Wrapper<ShopDiscussDO> wrapper = new EntityWrapper<ShopDiscussDO>();
       
        wrapper.orderBy("id", false);
        Integer count = shopDiscussMapper.selectCount(wrapper);
        List<ShopDiscussDO> ShopDiscussDOS = shopDiscussMapper.selectPage(new RowBounds((page - 1) * limit, limit), wrapper);
        return new Page<>(ShopDiscussDOS, page, limit, count);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ShopDiscussDO create(ShopDiscussDO ShopDiscussDO, Long adminId) throws ServiceException {
        ShopDiscussDO.setAdminId(adminId);
        Date now = new Date();
        ShopDiscussDO.setGmtCreate(now);
        ShopDiscussDO.setGmtUpdate(now);
        if(ShopDiscussDO.getFileList()!=null){
            Gson gson=new Gson();
            ShopDiscussDO.setFile(gson.toJson(ShopDiscussDO.getFileList()));
        }
        if (shopDiscussMapper.insert(ShopDiscussDO) > 0) {
            ShopDiscussDO.setId(ShopDiscussDO.getId());
            return ShopDiscussDO;
        }
        throw new AdminServiceException(ExceptionDefinition.ADMIN_UNKNOWN_EXCEPTION);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String update(ShopDiscussDO ShopDiscussDO, Long adminId) throws ServiceException {
        Long id = ShopDiscussDO.getId();
        if (id == null) {
            return "fail";
        }
        Date now = new Date();
        if(ShopDiscussDO.getFileList()!=null && ShopDiscussDO.getFileList().size()>0){
            Gson gson=new Gson();
            ShopDiscussDO.setFile(gson.toJson(ShopDiscussDO.getFileList()));
        }
        ShopDiscussDO.setGmtUpdate(now);
        if (shopDiscussMapper.updateById(ShopDiscussDO) > 0) {
            return "ok";
        }
        throw new AdminServiceException(ExceptionDefinition.ADMIN_UNKNOWN_EXCEPTION);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String delete(Long id, Long adminId) throws ServiceException {
        if (shopDiscussMapper.deleteById(id) > 0) {
            return "ok";
        }
        throw new AdminServiceException(ExceptionDefinition.ADMIN_UNKNOWN_EXCEPTION);

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String batchDelete(String idsJson, Long adminId) throws ServiceException {
        List<Long> ids = JSONObject.parseArray(idsJson, Long.class);
        if (CollectionUtils.isEmpty(ids)) {
            throw new AdminServiceException(ExceptionDefinition.ADMIN_UNKNOWN_EXCEPTION);
        }
        for (Long spuId : ids) {
            shopDiscussMapper.deleteById(spuId);
        }
        return "ok";
    }


    @Override
    public ShopDiscussDO detail(Long id, Long adminId) throws ServiceException {
        return shopDiscussMapper.selectById(id);
    }

    @Override
    public Boolean updateStatus(Long adminId, Long id, Integer status) throws ServiceException {
        if (status == null || (status != 0 && status != 1)) {
            throw new AdminServiceException(ExceptionDefinition.USER_INFORMATION_MISSING);
        }
        ShopDiscussDO ShopDiscussDO = new ShopDiscussDO();
        ShopDiscussDO.setId(id);
        ShopDiscussDO.setGmtUpdate(new Date());
        if (shopDiscussMapper.updateById(ShopDiscussDO) > 0) {
            return true;
        }
        return false;
    }

}